<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Encrypts plaintext with given parameters</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.mcrypt-enc-self-test.html">mcrypt_enc_self_test</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.mcrypt-generic-deinit.html">mcrypt_generic_deinit</a></div>
 <div class="up"><a href="ref.mcrypt.html">Mcrypt 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.mcrypt-encrypt" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mcrypt_encrypt</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.2, PHP 5)</p><p class="refpurpose"><span class="refname">mcrypt_encrypt</span> &mdash; <span class="dc-title">Encrypts plaintext with given parameters</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.mcrypt-encrypt-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>mcrypt_encrypt</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$cipher</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$key</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$data</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$mode</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$iv</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   Encrypts the data and returns it.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mcrypt-encrypt-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">cipher</code></em></dt>

     <dd>

      <p class="para"><strong><code>MCRYPT_ciphername</code></strong> 常量中的一个，或者是字符串值的算法名称。</p>
     </dd>

    
    
     <dt>
<em><code class="parameter">key</code></em></dt>

     <dd>

      <p class="para">
       The key with which the data will be encrypted. If it&#039;s smaller than
       the required keysize, it is padded with &#039;<em>\0</em>&#039;. It is
       better not to use ASCII strings for keys.
      </p>
      <p class="para">
       It is recommended to use the mhash functions to create a key from a
       string.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">data</code></em></dt>

     <dd>

      <p class="para">
       The data that will be encrypted with the given <em><code class="parameter">cipher</code></em>
       and <em><code class="parameter">mode</code></em>. If the size of the data is not n * blocksize,
       the data will be padded with &#039;<em>\0</em>&#039;.
      </p>
      <p class="para">
       The returned crypttext can be larger than the size of the data that was
       given by <em><code class="parameter">data</code></em>.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">mode</code></em></dt>

     <dd>

      <p class="para"><strong><code>MCRYPT_MODE_modename</code></strong> 常量中的一个，或以下字符串中的一个：&quot;ecb&quot;，&quot;cbc&quot;，&quot;cfb&quot;，&quot;ofb&quot;，&quot;nofb&quot; 和 &quot;stream&quot;。</p>
     </dd>

    
    
     <dt>
<em><code class="parameter">iv</code></em></dt>

     <dd>

      <p class="para">在 CBC, CFB, OFB 模式中用于初始化过程，在 STREAM 模式中用于某些算法。如果未提供 IV 并且在某算法中需要，本函数发出一条警告并使用一个全部字节设为 &quot;<em>\0</em>&quot; 的 IV。</p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mcrypt-encrypt-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the encrypted data, as a string.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mcrypt-encrypt-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-817">
    <p><strong>Example #1 <span class="function"><strong>mcrypt_encrypt()</strong></span> Example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;---&nbsp;ENCRYPTION&nbsp;---<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;the&nbsp;key&nbsp;should&nbsp;be&nbsp;random&nbsp;binary,&nbsp;use&nbsp;scrypt,&nbsp;bcrypt&nbsp;or&nbsp;PBKDF2&nbsp;to<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;convert&nbsp;a&nbsp;string&nbsp;into&nbsp;a&nbsp;key<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;key&nbsp;is&nbsp;specified&nbsp;using&nbsp;hexadecimal<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pack</span><span style="color: #007700">(</span><span style="color: #DD0000">'H*'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;show&nbsp;key&nbsp;size&nbsp;use&nbsp;either&nbsp;16,&nbsp;24&nbsp;or&nbsp;32&nbsp;byte&nbsp;keys&nbsp;for&nbsp;AES-128,&nbsp;192<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;and&nbsp;256&nbsp;respectively<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$key_size&nbsp;</span><span style="color: #007700">=&nbsp;&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Key&nbsp;size:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$key_size&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$plaintext&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"This&nbsp;string&nbsp;was&nbsp;AES-256&nbsp;/&nbsp;CBC&nbsp;/&nbsp;ZeroBytePadding&nbsp;encrypted."</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;create&nbsp;a&nbsp;random&nbsp;IV&nbsp;to&nbsp;use&nbsp;with&nbsp;CBC&nbsp;encoding<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$iv_size&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_get_iv_size</span><span style="color: #007700">(</span><span style="color: #0000BB">MCRYPT_RIJNDAEL_128</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MCRYPT_MODE_CBC</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$iv&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_create_iv</span><span style="color: #007700">(</span><span style="color: #0000BB">$iv_size</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MCRYPT_RAND</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;creates&nbsp;a&nbsp;cipher&nbsp;text&nbsp;compatible&nbsp;with&nbsp;AES&nbsp;(Rijndael&nbsp;block&nbsp;size&nbsp;=&nbsp;128)<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;to&nbsp;keep&nbsp;the&nbsp;text&nbsp;confidential&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;only&nbsp;suitable&nbsp;for&nbsp;encoded&nbsp;input&nbsp;that&nbsp;never&nbsp;ends&nbsp;with&nbsp;value&nbsp;00h<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;(because&nbsp;of&nbsp;default&nbsp;zero&nbsp;padding)<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ciphertext&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_encrypt</span><span style="color: #007700">(</span><span style="color: #0000BB">MCRYPT_RIJNDAEL_128</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$plaintext</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MCRYPT_MODE_CBC</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$iv</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;prepend&nbsp;the&nbsp;IV&nbsp;for&nbsp;it&nbsp;to&nbsp;be&nbsp;available&nbsp;for&nbsp;decryption<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ciphertext&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$iv&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$ciphertext</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;encode&nbsp;the&nbsp;resulting&nbsp;cipher&nbsp;text&nbsp;so&nbsp;it&nbsp;can&nbsp;be&nbsp;represented&nbsp;by&nbsp;a&nbsp;string<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ciphertext_base64&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">base64_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$ciphertext</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&nbsp;</span><span style="color: #0000BB">$ciphertext_base64&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;===&nbsp;WARNING&nbsp;===<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;Resulting&nbsp;cipher&nbsp;text&nbsp;has&nbsp;no&nbsp;integrity&nbsp;or&nbsp;authenticity&nbsp;added<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;and&nbsp;is&nbsp;not&nbsp;protected&nbsp;against&nbsp;padding&nbsp;oracle&nbsp;attacks.<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;---&nbsp;DECRYPTION&nbsp;---<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ciphertext_dec&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">base64_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$ciphertext_base64</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;retrieves&nbsp;the&nbsp;IV,&nbsp;iv_size&nbsp;should&nbsp;be&nbsp;created&nbsp;using&nbsp;mcrypt_get_iv_size()<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$iv_dec&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$ciphertext_dec</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$iv_size</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;retrieves&nbsp;the&nbsp;cipher&nbsp;text&nbsp;(everything&nbsp;except&nbsp;the&nbsp;$iv_size&nbsp;in&nbsp;the&nbsp;front)<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ciphertext_dec&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$ciphertext_dec</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$iv_size</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;may&nbsp;remove&nbsp;00h&nbsp;valued&nbsp;characters&nbsp;from&nbsp;end&nbsp;of&nbsp;plain&nbsp;text<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$plaintext_dec&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_decrypt</span><span style="color: #007700">(</span><span style="color: #0000BB">MCRYPT_RIJNDAEL_128</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ciphertext_dec</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MCRYPT_MODE_CBC</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$iv_dec</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&nbsp;</span><span style="color: #0000BB">$plaintext_dec&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Key size: 32
ENJW8mS2KaJoNB5E5CoSAAu0xARgsR1bdzFWpEn+poYw45q+73az5kYi4j+0haevext1dGrcW8Qi59txfCBV8BBj3bzRP3dFCp3CPQSJ8eU=
This string was AES-256 / CBC / ZeroBytePadding encrypted.
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   See also <span class="function"><a href="function.mcrypt-module-open.html" class="function">mcrypt_module_open()</a></span> for a more advanced API
   and an example.
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.mcrypt-enc-self-test.html">mcrypt_enc_self_test</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.mcrypt-generic-deinit.html">mcrypt_generic_deinit</a></div>
 <div class="up"><a href="ref.mcrypt.html">Mcrypt 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
